home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Tool Chest / QuickDraw / Virtual Sphere 1.0.1 / Virtual Sphere Sample Code 1.1 / MyMath.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-25  |  3.1 KB  |  113 lines  |  [TEXT/MPS ]

  1. /*•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  2. /* MyMath.c
  3. /*
  4. /* Implementations to hide all the ugliness when using floating point and fixed point
  5. /* math between MPW C and Think C.  Instead of figuring out when to and when
  6. /* not to include SANE.h and Math.h, and their order, just include MyMath.h.
  7. /*
  8. /* This file contains routines that are missing when compiling under Think C
  9. /* with different compiler options.  This file is not needed really for MPW C.
  10. /* Note asin and atan2 are no longer needed in release 1.1 of the sample code.
  11. /* They have been left in case you need them for your projects.
  12. /*
  13. /* Author: Michael Chen, Human Interface Group / ATG
  14. /* Copyright © 1991-1993 Apple Computer, Inc.  All rights reserved.
  15. /*
  16. /* Part of Virtual Sphere Sample Code Release v1.1
  17. /*•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••*/
  18.  
  19. #ifndef __MyMath__
  20. #include "MyMath.h"
  21. #endif
  22.  
  23. #ifndef __FIXMATH__
  24. #include <FixMath.h>
  25. #endif
  26.  
  27. #define    kPi        3.14159265358979323846
  28.  
  29. #ifdef THINK_C
  30.  
  31. extern void x80tox96(void *x80, void *x96);
  32. extern void x96tox80(void *x96, void *x80);
  33.  
  34. /*=================================================================================================
  35. /* asin for Think C since their SANE did not implement asin
  36. /*
  37. /* Code adapted from Think C's Clibraries:Sources:math.c
  38. /*-------------------------------------------------------------------------------------------------*/
  39. #if !__option(mc68881)
  40. pascal Real asin (Real x)
  41. {
  42.     Real y = fabs(x);
  43.  
  44.     if (y > .5) {
  45.         y = 1 - y;
  46.         y = 2 * y - y * y;
  47.     }
  48.     else
  49.         y = 1 - y * y;
  50.     return(atan(x / sqrt(y)));
  51. }
  52. #endif //!__option(mc68881)
  53.  
  54. /*=================================================================================================
  55. /* atan2 for Think C since their SANE did not implement atan2
  56. /*
  57. /* Code adapted from Think C's Clibraries:Sources:math.c
  58. /*-------------------------------------------------------------------------------------------------*/
  59. #if !__option(mc68881)
  60. pascal Real atan2 (Real y, Real x)
  61. {
  62.     Real    z;
  63.  
  64.     if (x == 0 && y == 0) {
  65.         if (qDebug)  ("\p atan2: both arguements are zero");
  66.         return (0);
  67.     }
  68.     z = atan (y / x);
  69.     if (x < 0) {
  70.         if (y < 0)
  71.             z -= kPi;
  72.         else
  73.             z += kPi;
  74.     }
  75.     return(z);
  76. }
  77. #endif //!__option(mc68881)
  78.  
  79.  
  80. /*=================================================================================================
  81. /* Real2Fix for Think C when using "Generate 68881 instructions"
  82. /*-------------------------------------------------------------------------------------------------*/
  83. #if __option(mc68881)
  84. pascal Fixed Real2Fix (Real a)
  85. {
  86.     Fixed        result;
  87.     extended    temp;
  88.     
  89.     x96tox80 (&a, &temp);
  90.     return (X2Fix (temp));
  91. }
  92. #endif //__option(mc68881)
  93.  
  94. /*=================================================================================================
  95. /* Fix2Real for Think C when using "Generate 68881 instructions"
  96. /*-------------------------------------------------------------------------------------------------*/
  97. #if __option(mc68881)
  98. pascal Real Fix2Real (Fixed a)
  99. {
  100.     Real        result;
  101.     extended    temp;
  102.     
  103.     temp = Fix2X (a);
  104.     x80tox96 (&temp, &result);
  105.     return (result);
  106. }
  107. #endif //__option(mc68881)
  108.  
  109.  
  110.  
  111. #endif THINK_C
  112.  
  113.